package com.wnhz.work.jy.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.wnhz.work.jy.R;
import com.wnhz.work.jy.bean.ManageMoneyBean;
import com.wnhz.work.jy.utils.DisplayUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/11/25 0025.
 */
public class MarqueeView2 extends ViewFlipper {
    private Context mContext; // 上下文
    private List<ManageMoneyBean> notices; // 通知字符串集合
    private boolean isSetAnimDuration = false;
    private OnItemClickListener onItemClickListener; // 某个通知item被点击监听器
    // item实际上是一个textview

    private int interval = 2000;
    private int animDuration = 500;
    private int textSize = 14;
    private int textColor = 0xff008586;

    public MarqueeView2(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr) {
        this.mContext = context;
        if (notices == null) {
            notices = new ArrayList<>();
        }

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MarqueeViewStyle, defStyleAttr,
                0);
        interval = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvInterval, interval);
        isSetAnimDuration = typedArray.hasValue(R.styleable.MarqueeViewStyle_mvAnimDuration);
        animDuration = typedArray.getInteger(R.styleable.MarqueeViewStyle_mvAnimDuration, animDuration);
        if (typedArray.hasValue(R.styleable.MarqueeViewStyle_mvTextSize)) {
            textSize = (int) typedArray.getDimension(R.styleable.MarqueeViewStyle_mvTextSize, textSize);
            textSize = DisplayUtil.px2sp(mContext, textSize);
        }
        textColor = typedArray.getColor(R.styleable.MarqueeViewStyle_mvTextColor, textColor);
        typedArray.recycle();

        setFlipInterval(interval);

        Animation animIn = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_in);
        if (isSetAnimDuration)
            animIn.setDuration(animDuration);
        setInAnimation(animIn);

        Animation animOut = AnimationUtils.loadAnimation(mContext, R.anim.anim_marquee_out);
        if (isSetAnimDuration)
            animOut.setDuration(animDuration);
        setOutAnimation(animOut);
    }


    // 根据公告字符串列表启动轮播
    public void startWithList(List<ManageMoneyBean> notices) {
        System.out.println(notices.size() + "qqqqqqqqqq3");
        setNotices(notices);
        start();
    }

    // 根据宽度和公告字符串启动轮播
    private void startWithFixedWidth() {
        start();
    }

    // 启动轮播
    public boolean start() {
        if (notices == null || notices.size() == 0)
            return false;
        removeAllViews();

        for (int i = 0; i < notices.size(); i++) {
            System.out.println(notices.size() + "qqqqqqqqqq5");
            final View textView = createView(notices.get(i));
            final int finalI = i;
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(finalI, textView);
                    }
                }
            });
            addView(textView);
        }

        if (notices.size() > 1) {
            startFlipping();
        }
        return true;
    }

    // 创建ViewFlipper下的TextView
    private View createView(ManageMoneyBean bean) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_marqueeview2, null);
        ImageView iv_pic = (ImageView) view.findViewById(R.id.iv_pic);
        TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
        TextView tv_number_price = (TextView) view.findViewById(R.id.tv_number_price);

        int price = Integer.parseInt(bean.getGains());
        if (price < 0) {
            tv_number_price.setTextColor(0xffff0000);
            tv_number_price.setText(bean.getGains() + "元");
            iv_pic.setImageResource(R.mipmap.ic_zoushidown);
        } else {
            tv_number_price.setText("+" + bean.getGains() + "元");
            iv_pic.setImageResource(R.mipmap.ic_zoushiup);
        }
        tv_name.setText(bean.getTitle());


        return view;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public List<ManageMoneyBean> getNotices() {
        return notices;
    }

    public void setNotices(List<ManageMoneyBean> notices) {

        this.notices.clear();
        this.notices.addAll(notices);
        System.out.println(this.notices.size() + "qqqqqqqqqq4");
    }

    public interface OnItemClickListener {
        void onItemClick(int position, View v);
    }
}
